<template>
	<view class="myPages" :style="{paddingTop: `${search.top+search.height+10}px`}">
		<myHeard :search="search" @goPath="goBack" :title="title"></myHeard>

		<view class="myPages__main" v-if="addressList.length==0">
			<image class="kong" src="/static/images/addAdressIcon.png"></image>
		</view>
		<view class="myPages__main" v-else>
			<view v-for="(item,index) in addressList" :key="index">
				<view class="myPages__mainItem" v-if="isProfile">
					<view class="myPages__mainItemTop">
						<view class="myPages__mainItemTopLeft">
							<p>{{item.province}}{{item.city}}{{item.region}}{{item.detail}}</p>
							<view class="myPages__mainItemTopLeftMain">
								<span>{{item.name}}</span>
								<span>{{item.phone}}</span>
							</view>
						</view>
						<view class="myPages__mainItemTopRight" @click.stop="goPath(item)">
							<image class="myPages__mainItemTopRightImg" src="/static/images/bianjiIcon.png"></image>
						</view>
					</view>
					<view class="myPages__mainItemButtom">
						<view @click.stop="updataIndex(item,index)" class="myPages__mainItemButtomLeftBox">
							<image class="myPages__mainItemButtomLeftBoxImg"
								:src="item.isDefault ? '/static/images/catIcon_active.png' : '/static/images/catIcon.png'">
							</image>
							<span>设为默认地址</span>
						</view>
						<view @click.stop="remol(item.addressId,index)" class="myPages__mainItemButtomRightBox">
							删除
						</view>
					</view>
				</view>
				<view class="myPages__mainItem" v-else @click.stop="updataIndex(item,index)">
					<view class="myPages__mainItemTop">
						<view class="myPages__mainItemTopLeft">
							<p>{{item.province}}{{item.city}}{{item.region}}{{item.detail}}</p>
							<view class="myPages__mainItemTopLeftMain">
								<span>{{item.name}}</span>
								<span>{{item.phone}}</span>
							</view>
						</view>
						<view class="myPages__mainItemTopRight" @click.stop="goPath(item)">
							<image class="myPages__mainItemTopRightImg" src="/static/images/bianjiIcon.png"></image>
						</view>
					</view>
					<view class="myPages__mainItemButtom">
						<view class="myPages__mainItemButtomLeftBox">
							<image class="myPages__mainItemButtomLeftBoxImg"
								:src="item.isDefault ? '/static/images/catIcon_active.png' : '/static/images/catIcon.png'">
							</image>
							<span>设为默认地址</span>
						</view>
						<view @click.stop="remol(item.addressId,index)" class="myPages__mainItemButtomRightBox">
							删除
						</view>
					</view>
				</view>
			</view>

		</view>
		<u-overlay :show="show" @click="show = false">
			<view @click.stop="" class="warp">
				<view class="warpText">确认删除收货地址？</view>
				<view class="warpButtom">
					<span class="warpButtomQx" @click.stop="show = false">取消</span>
					<span class="warpButtomYes" @click.stop="warpButtomYes">确定</span>
				</view>
			</view>
		</u-overlay>
		<!-- 删除成功 -->
		<u-overlay :opacity="0.01" :show="isShowDui">
			<view @click.stop="" class="overlayBox">
				<image src="/static/images/duiIcon.png"></image>
				<span>删除成功</span>
			</view>
		</u-overlay>
		<view class="myPages__remarkButtom">
			<view class="myPages__remarkBoxs" @click="goPath(false)">
				<image class="myPages__remarkBoxsIcon" src="/static/images/adddIcon.png"></image>
				<span>添加新地址</span>
			</view>
		</view>
	</view>
</template>

<script>
	import myHeard from '@/components/myHeard/index.vue'
	export default {
		components: {
			myHeard
		},
		data() {
			return {
				actionIndex: 0,
				show: false,
				title: '收货地址',
				addressList: [],
				remoeIndex: 0,
				goPlus: false,
				newAddressList: null,
				isProfile: false,
				isShowDui: false

			}
		},
		onLoad(oprtion) {
			// #ifndef H5
			let search = uni.getMenuButtonBoundingClientRect()
			this.$store.commit('updataSearch', search);
			// #endif
			if (oprtion.goPlus) {
				this.goPlus = oprtion.goPlus;
			}
		},
		onShow() {
			this.initMemberAddressWxList();
			this.isProfile = uni.getStorageSync('isProfile');
		},
		computed: {
			search() {
				return this.$store.state.search;
			},

		},
		methods: {
			warpButtomYes() {

				uni.showLoading({
					title: '加载中',
					mask: true
				});
				this.$api.memberAddressWxRemove([this.actionIndex]).then(res => {

					if (res.code == 200) {
						let nowaddressList = JSON.parse(JSON.stringify(this.addressList));
						nowaddressList.splice(this.remoeIndex, 1);
						this.addressList = JSON.parse(JSON.stringify(nowaddressList));
						// uni.showToast({
						// 	icon:'none',
						// 	title: res.msg,
						// 	mask:true
						// })
						this.isShowDui = true;
						this.show = false;
						setTimeout(() => {
							this.isShowDui = false;
						}, 1000)
					}
				})
			},
			initMemberAddressWxList() {

				uni.showLoading({
					title: '加载中',
					mask: true
				});
				this.$api.memberAddressWxList().then(res => {
					if (res.code == 200) {
						this.addressList = res.data;

					}
				})
			},
			updataIndex(item, index) {
				this.actionIndex = item.addressId;
				this.$api.memberAddressWxDefault({
					addressId: item.addressId
				}).then(res => {
					if (res.code == 200) {
						let nowaddressList = JSON.parse(JSON.stringify(this.addressList));
						nowaddressList.forEach(item => {
							item.isDefault = false;
						})
						nowaddressList[index].isDefault = true;
						this.addressList = JSON.parse(JSON.stringify(nowaddressList));
						uni.showToast({
							icon: 'none',
							title: res.msg,
							mask: true
						});
						let pages = getCurrentPages();
						let prevPage = pages[pages.length - 2];
						this.newAddressList = JSON.parse(JSON.stringify(item));
						prevPage.onShow(item);

						setTimeout(() => {
							uni.navigateBack({
								delta: 1,
							});
						}, 1000)

					}
				})
			},
			remol(addressId, index) {
				this.actionIndex = addressId;
				this.remoeIndex = index;
				this.show = true;
			},
			goPath(item) {

				let url = item ? `/package/amendProfile/index?obj=${JSON.stringify(item)}` : `/package/amendProfile/index`;
				uni.navigateTo({
					url
				})
			},
			goBack() {
				if (this.goPlus) {
					if (this.addressList.length != 0) {
						for (let i = 0; i < this.addressList.length; i++) {
							let item = this.addressList[i];
							if (item.isDefault) {
								this.newAddressList = item;
								break;
							}
						}
					} else {
						this.newAddressList = '无';
					}



					let pages = getCurrentPages();
					let prevPage = pages[pages.length - 2];

					this.newAddressList = this.newAddressList ? this.newAddressList : '无';
					console.log('this.newAddressList--->', this.newAddressList)
					prevPage.onShow(this.newAddressList);

					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
						});
					}, 1000)
				} else {
					uni.navigateBack()
				}

			}
		}
	}
</script>

<style scoped lang="scss">
	.myPages {
		min-height: 100vh;
		background-color: #F7F8FC;
		box-sizing: border-box;

		.kong {
			width: 408rpx;
			height: 408rpx;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
		}

		.myPages__main {
			padding: 32rpx;
			box-sizing: border-box;
			width: 100%;
			padding-bottom: 176rpx;
		}

		.myPages__mainItemButtomLeftBox {
			display: flex;
			align-items: center;
			flex: 1;

			span {
				height: 36rpx;
				font-size: 26rpx;
				font-family: 'DIN';
				font-weight: normal;
				line-height: 36rpx;
				color: #B0B1BB;
				margin-left: 16rpx;
			}
		}

		.overlayBox {
			width: 202rpx;
			height: 90rpx;
			background: rgba(0, 0, 0, 0.9);
			border-radius: 32px;
			display: flex;
			align-items: center;
			justify-content: center;
			position: fixed;
			top: 48%;
			left: 50%;
			transform: translate(-50%, -50%);

			image {
				display: inline-block;
				width: 34rpx;
				height: 34rpx;
			}

			span {
				display: inline-block;
				height: 34rpx;
				font-size: 24rpx;
				font-family: 蘋方-簡;
				font-weight: normal;
				line-height: 34rpx;
				color: #FFFFFF;
				margin-left: 8rpx;

			}
		}

		.warp {
			width: 70%;
			height: 210rpx;
			text-align: center;
			border-radius: 40rpx;
			background-color: #FFFFFF;
			overflow: hidden;
			position: fixed;
			top: 40%;
			left: 50%;
			transform: translate(-50%);
		}

		.warpText {
			font-size: 28rpx;
			font-family: 'DIN';
			font-weight: normal;
			color: #2B2B33;
			width: 100%;
			padding: 40rpx 0;
		}

		.warpButtom {
			display: flex;

			span {
				flex: 1;
				height: 90rpx;
				line-height: 90rpx;
				font-size: 28rpx;
				font-family: 'DIN';
				font-weight: normal;
				color: #F0414D;
				border-top: 3rpx solid #F0F2F8;
				box-sizing: border-box;
			}

			span:nth-child(1) {
				border-right: 3rpx solid #F0F2F8;
				color: #B0B1BB;
			}
		}

		.myPages__mainItemButtom {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.myPages__mainItem {
			width: 100%;
			height: 200rpx;
			background: rgba(255, 255, 255, 0.39);
			box-shadow: 0px 0px 40rpx rgba(228, 228, 245, 0.4);
			opacity: 1;
			border-radius: 40rpx;
			padding: 32rpx;
			padding-bottom: 16rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-bottom: 20rpx;
		}

		.myPages__mainItemTopRightImg {
			width: 40rpx;
			height: 40rpx;
			float: right;
		}

		.myPages__mainItemButtomLeftBoxImg {
			width: 32rpx;
			height: 32rpx;
		}

		.myPages__mainItemButtomRightBox {
			height: 36rpx;
			font-size: 26rpx;
			font-family: 'DIN';
			font-weight: normal;
			line-height: 36rpx;
			color: #F0414D;
			flex: 0 0 60rpx;
		}

		.myPages__mainItemTopRight {
		 flex: 0 0 58rpx;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 58rpx;
			border-left: 4rpx solid #F0F2F8;
			box-sizing: border-box;

		}

		.myPages__mainItemTopLeft {
			flex: 1;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;

			p {
				height: 40rpx;
				font-size: 28rpx;
				font-family: 'DIN';
				font-weight: normal;
				line-height: 40rpx;
				color: #2B2B33;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}

			span {
				display: inline-block;
				height: 34rpx;
				font-size: 24rpx;
				font-family: 'DIN';
				font-weight: normal;
				line-height: 34rpx;
				color: #7B7D8D;
			}

			span:nth-child(2) {
				margin-left: 20rpx;
			}
		}

		.myPages__mainItemTop {
			display: flex;
			align-items: center;
			padding-bottom: 16rpx;
			border-bottom: 3rpx solid #F0F2F8;

		}

		.myPages__remarkButtom {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
			padding: 0 32rpx;
			padding-bottom: 48rpx;
			box-sizing: border-box;
			background-color: #F7F8FC;
			z-index: 9;
			display: flex;
			justify-content: center;
			align-items: center;

			.myPages__remarkBoxsIcon {
				width: 28rpx;
				height: 28rpx;
				margin-right: 4rpx;
			}
		}

		.myPages__remarkBoxs {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: #F44B5B;
			opacity: 1;
			border-radius: 40rpx;
			font-size: 28rpx;
			font-family: 'DIN';
			font-weight: normal;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
			box-shadow: 0 28rpx 22rpx -26rpx #F44B5B;

		}
	}
</style>
